<template>
  <div class="table-box">
    <KingTable ref="kingTable" :tableOptions="_tableOptions" @handle-btn-click="handleBtnAction">
      <!-- 表格 header 按钮 -->
      <template #tableHeaderBtnAfter>
        <UploadDialog
          :accept="'.xls,.xlsx'"
          :limit="3"
          :size="5242880"
          templatefilename="人员档案导入模板.xlsx"
          :url="'system/person/importFile'"
          @callbackupload="fetchTableData"
        >
          <template #uploadLink>
            <el-button :icon="Upload" style="margin-left: 12px" type="primary"> 导入 </el-button>
          </template>
        </UploadDialog>
        <ExportDirect expfilename="人员档案导出模板" :params="_initParam" url="system/person">
          <template #downloadLink>
            <el-button :icon="Download" style="margin-left: 12px" type="primary"> 导出 </el-button>
          </template>
        </ExportDirect>
      </template>
      <!-- 表格操作 -->
      <template #operation="scope">
        <TableRowBtns :row="scope.row" @handle-row-action="handleBtnAction" />
      </template>
    </KingTable>
    <edit ref="editRef" @on-success="fetchTableData" />
    <info ref="infoRef" />
  </div>
</template>

<script setup lang="tsx" name="PersonList">
import { ref, reactive } from "vue";
import { Download, Upload } from "@element-plus/icons-vue";
import KingTable from "@/components/KingTable/index.vue";
import TableRowBtns from "@/components/KingTable/components/TableRowBtns.vue";
import { operateType } from "@/plugins/commonType.enum";
import SelectVxeTreeOne from "@/plugins/tree/SelectVxeTreeOne.vue";
import UploadDialog from "@/plugins/upload/UploadDialog.vue";
import ExportDirect from "@/plugins/export/ExportDirect.vue";
import edit from "./components/personEdit.vue";
import info from "./components/personInfo.vue";

const infoRef = ref();
const editRef = ref();

// 获取 KingTable 元素，调用其获取刷新数据方法（还能获取到当前查询参数，方便导出携带参数）
const kingTable = ref();

// 如果表格需要初始化请求参数，直接定义传给 KingTable(之后每次请求都会自动带上该参数，此参数更改之后也会一直带上，改变此参数会自动刷新表格数据)
const _initParam = reactive({ EQ_ss: 1 });

// 表格配置项
const _tableCols = [
  { type: "selection", fixed: "left", align: "center", width: 60 },
  { type: "index", label: "序号", align: "center", width: 60 },
  { label: "科室", prop: "orgname", width: 100 },
  { label: "姓名", prop: "name", width: 100 },
  { label: "身份证号码", prop: "idcard", width: 200 },
  { label: "性别", prop: "sex", dictCode: "SexEnum", width: 60 },
  { label: "岗位", prop: "post", dictCode: "Post", width: 160 },
  { label: "岗位级别", prop: "postlevel", dictCode: "PostLevel", width: 160 },
  { label: "薪级", prop: "salary", dictCode: "SalaryLevel", width: 100 },
  { label: "学历", prop: "education", dictCode: "Education", width: 100 },
  { label: "手机号码", prop: "mobile", width: 160 },
  { prop: "operation", label: "操作", fixed: "right", width: 260 }
];

const _searchCols = [
  {
    prop: "EQ_orgid",
    label: "机构",
    width: 100,
    render: ({ searchParam }: { searchParam: any }) => {
      return (
        <div class="flx-center">
          <SelectVxeTreeOne
            v-model={searchParam.EQ_orgid}
            placeholder="请选择机构"
            url="system/org/query"
            query-form={{ sorts: "cts@asc" }}
            transfer
          />
        </div>
      );
    }
  },
  {
    prop: "LIKE_name",
    label: "姓名",
    width: 100,
    el: "input"
  },
  {
    prop: "EQ_sex",
    label: "性别",
    width: 100,
    el: "select",
    dictCode: "sys_user_sex"
  },
  {
    prop: "EQ_post",
    label: "岗位级别",
    width: 100,
    el: "select",
    dictCode: "PostLevel"
  },
  {
    prop: "EQ_salary",
    label: "薪级",
    width: 100,
    el: "select",
    dictCode: "SalaryLevel"
  },
  {
    prop: "EQ_education",
    label: "学历",
    width: 100,
    el: "select",
    dictCode: "Education"
  }
];

const _tableOptions = {
  title: "人员管理",
  queryUrl: "system/person/query",
  operUrl: "system/person",
  tableColumns: _tableCols,
  searchColumns: _searchCols,
  initParam: _initParam
};

// 重新拉取表格数据
const fetchTableData = () => {
  kingTable.value?.getTableList();
};

/**
 * 增删查按钮执行的业务操作
 * @param {*} type 当前执行操作的类型
 * @param {*} row
 */
const handleBtnAction = async (type: string, row: any) => {
  switch (type) {
    case operateType.ADD: // 新增操作
      editRef.value.showDialog(row, type);
      break;
    case operateType.EDIT: // 编辑操作
      editRef.value.showDialog(row, type);
      break;
    case operateType.DETAIL: // 查询操作
      infoRef.value.showDialog(row);
      break;
    case operateType.DEL: // 删除操作
      kingTable.value?.handleDelete(row);
      break;
    case operateType.BATCH_DEL: // 批量删除操作
      kingTable.value?.handleBatchDelete();
      break;
  }
};
</script>
